package com.wl.reflection;

import java.lang.annotation.*;
import java.lang.reflect.Field;

public class Test02 {
    public static void main(String[] args) throws 
            ClassNotFoundException, NoSuchFieldException {
        Class<?> c1 = Class.forName("com.wl.reflection.Student");

        //通过反射获取注解
        Annotation[] annotations = c1.getAnnotations();
        for (Annotation annotation : annotations) {
            System.out.println("annotation = " + annotation);
        }

        //获取注解的value
        MyAnnotation myAnnotation = c1.getAnnotation(MyAnnotation.class);
        System.out.println("myAnnotation.value() = " + myAnnotation.value());

        //获取类指定的注解
        Field name = c1.getDeclaredField("name");
        FiledAnnotation filedAnnotation = name.getAnnotation(FiledAnnotation.class);
        System.out.println("filedAnnotation.columnName() = " + filedAnnotation.columnName());
        System.out.println("filedAnnotation.type() = " + filedAnnotation.type());
        System.out.println("filedAnnotation.len() = " + filedAnnotation.len());
    }
}

@MyAnnotation("王磊")
class Student {
    @FiledAnnotation(columnName = "stu_name",type = "varchar", len = 100)
    private String name;
    @FiledAnnotation(columnName = "stu_id",type = "int", len = 10)
    private int sid;

}

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation{
    String value();
}
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface FiledAnnotation{
    String columnName();
    String type();
    int len();
}